package com.qiaoba.module.system.controller;

import com.github.pagehelper.PageHelper;
import com.qiaoba.api.log.annotation.Log;
import com.qiaoba.api.log.enums.BusinessType;
import com.qiaoba.api.system.entity.dto.SysRoleDTO;
import com.qiaoba.common.base.entity.AjaxResult;
import com.qiaoba.common.base.entity.Result;
import com.qiaoba.common.base.validate.AddGroup;
import com.qiaoba.common.base.validate.EditGroup;
import com.qiaoba.common.database.entity.PageQuery;
import com.qiaoba.common.database.entity.PageResult;
import com.qiaoba.module.system.entity.SysRole;
import com.qiaoba.module.system.entity.dto.DataScopeDTO;
import com.qiaoba.module.system.entity.dto.RoleStatusDTO;
import com.qiaoba.module.system.entity.param.SysDeptParam;
import com.qiaoba.module.system.entity.param.SysRoleParam;
import com.qiaoba.module.system.service.SysDeptService;
import com.qiaoba.module.system.service.SysRoleService;
import com.qiaoba.poi.excel.util.ExcelUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.Size;
import java.util.List;

/**
 * 角色管理 Web层
 *
 * @author ailanyin
 * @date 2023-09-12 20:41:22
 */
@Validated
@RestController
@RequestMapping("/system/role")
@RequiredArgsConstructor
@Tag(name = "角色管理")
public class SysRoleController {

    private final SysRoleService sysRoleService;
    private final SysDeptService sysDeptService;

    @PreAuthorize("hasAuthority('system:role:list')")
    @GetMapping("/list")
    @Operation(summary = "获取列表")
    public PageResult<SysRole> list(@Validated @ParameterObject SysRoleParam param, @ParameterObject PageQuery pageQuery) {
        PageHelper.startPage(pageQuery.getPageNum(), pageQuery.getPageSize());
        List<SysRole> list = sysRoleService.selectList(param);
        return PageResult.build(list);
    }

    @PreAuthorize("hasAuthority('system:role:query')")
    @GetMapping(value = "/{roleId}")
    @Operation(summary = "获取详情")
    public Result<SysRole> getInfo(@Size(max = 20, message = "ID不合法") @PathVariable String roleId) {
        return Result.success(sysRoleService.selectById(roleId, false));
    }

    @Log(title = "角色管理", businessType = BusinessType.INSERT)
    @PreAuthorize("hasAuthority('system:role:add')")
    @PostMapping
    @Operation(summary = "添加角色")
    public Result add(@Validated(AddGroup.class) @RequestBody SysRoleDTO dto) {
        return Result.toAjax(sysRoleService.insert(dto));

    }

    @Log(title = "角色管理", businessType = BusinessType.UPDATE)
    @PreAuthorize("hasAuthority('system:role:edit')")
    @PutMapping
    @Operation(summary = "修改角色")
    public Result edit(@Validated(EditGroup.class) @RequestBody SysRoleDTO dto) {
        return Result.toAjax(sysRoleService.updateById(dto));

    }

    @Log(title = "角色管理", businessType = BusinessType.DELETE)
    @PreAuthorize("hasAuthority('system:role:remove')")
    @DeleteMapping("/{roleIds}")
    @Operation(summary = "删除角色")
    public Result remove(@PathVariable List<String> roleIds) {
        return Result.toAjax(sysRoleService.deleteByIds(roleIds));
    }

    @Log(title = "角色管理", businessType = BusinessType.EXPORT)
    @PreAuthorize("hasAuthority('system:role:export')")
    @PostMapping("/export")
    @Operation(summary = "导出列表")
    public void export(HttpServletResponse response, @Validated SysRoleParam param) {
        List<SysRole> list = sysRoleService.selectList(param);
        ExcelUtil.exportExcel(list, SysRole.class, "角色数据", response);
    }

    @Log(title = "角色管理", businessType = BusinessType.UPDATE)
    @PreAuthorize("hasAuthority('system:role:edit')")
    @PutMapping("/changeStatus")
    @Operation(summary = "更改状态")
    public Result changeStatus(@RequestBody RoleStatusDTO dto) {
        return Result.toAjax(sysRoleService.changeStatus(dto.getRoleId(), dto.getStatus()));
    }

    @GetMapping(value = "/dept-tree/{roleId}")
    @Operation(summary = "角色部门树")
    public AjaxResult deptTree(@PathVariable("roleId") String roleId, SysDeptParam param) {
        AjaxResult ajax = AjaxResult.success();
        ajax.put("checkedKeys", sysDeptService.selectDeptIdsByRoleId(roleId));
        ajax.put("depts", sysDeptService.buildDeptTree(sysDeptService.selectList(param)));
        return ajax;
    }

    @Log(title = "角色管理", businessType = BusinessType.UPDATE)
    @PreAuthorize("hasAuthority('system:role:edit')")
    @PutMapping("/data-scope")
    @Operation(summary = "数据权限")
    public Result dataScope(@RequestBody DataScopeDTO dto) {
        return Result.toAjax(sysRoleService.updateDataScope(dto));
    }

}
